<template>
	<view class="content">
		<up-navbar class="head-index" :fixed="false" :safeAreaInsetTop="true" bgColor="#51d0d0" titleStyle="color:#fff"
			:autoBack="false" leftIcon=" " title="应用授权信息">
			<template #left>
				<up-icon name="arrow-left" color="#fff" size="20" @click="ClosePage"></up-icon>
			</template>

			<!-- #ifndef MP-WEIXIN -->

			<template #right>
				<m-capsule :closeHandler="ClosePage"></m-capsule>
			</template>
			<!-- #endif -->
		</up-navbar>

		<view class="MainBox">
			<view class="CardBox">
				<view class="TitleText">
					<text>APP授权信息</text>
				</view>

				<view class="EmpowerBox">
					<view v-if="!formData.IsTrue" @click="ToggleBtn" class="TextBtn">
						<text>申请授权</text>
					</view>

					<view v-else class="TextBtn">
						<text>已授权</text>
					</view>
				</view>

				<view class="CardMain">

					<view class="Top_isTrue_Text">
						<uni-icons :type="!formData.IsTrue ?  'locked' : 'calendar'"
							:color="!formData.IsTrue ?  '#ff0000' : '#3ed180'" size="60"></uni-icons>

						<view>
							<text v-if="!formData.IsTrue" class="isTrue_Text">本机未授权，请先前往申请授权</text>
							<text v-else class="isTrue_Text">本机已授权，允许使用软件</text>
							<text class="Text">软件授权</text>
						</view>
					</view>

					<view class="Top_Version_Box">
						<text class="TitleText">当前版本</text>
						<text class="Text">{{formData.version}}</text>
					</view>

					<view class="Top_Status_Box">
						<text class="TitleText">授权内容</text>

						<view v-if="!formData.IsTrue" class="Status_Box">
							<!-- <uni-icons color="#ff0000" custom-prefix="iconfont" type="icon-notquanshouquan"
								size="20"></uni-icons> -->

							<uni-icons fontFamily="CustomFontPlatform" color="#ff0000"
								:size="20">{{uni.$utils.toUniCode('&#xe70b;')}}</uni-icons>
							<text>当前版本未授权</text>
						</view>

						<view v-else class="Status_Box">
							<!-- <uni-icons type="checkmarkempty" color="#3ed180" size="20"></uni-icons> -->
							<!-- <uni-icons color="#3ed180" custom-prefix="iconfont" type="icon-shouquan1"
								size="20"></uni-icons> -->

							<uni-icons fontFamily="CustomFontPlatform" color="#3ed180"
								:size="20">{{uni.$utils.toUniCode('&#xe602;')}}</uni-icons>
							<text>当前版本已授权</text>
						</view>
					</view>

					<view class="Top_MachineCode_Box" @click="uni.$utils.TextCopyFun(formData.OnlyIdentity)">
						<text class="TitleText">当前设备机器码</text>

						<view class="MachineCode_Box">
							<!-- <uni-icons color="#333" custom-prefix="iconfont" type="icon-jiqima" size="28"></uni-icons> -->

							<uni-icons fontFamily="CustomFontPlatform" color="#333"
								:size="28">{{uni.$utils.toUniCode('&#xe84d;')}}</uni-icons>

							<text style="margin-left: 10rpx;">{{formData.OnlyIdentity}}</text>


							<uni-icons v-if="formData.OnlyIdentity" fontFamily="CustomFontPlatform" color="#0647c1"
								style="margin-left: 4rpx;" size="16">{{uni.$utils.toUniCode('&#xe862;')}}</uni-icons>
						</view>
					</view>

				</view>

			</view>


			<view v-show="formData.IsTrue" class="CardBox">
				<view class="TitleText">
					<text>授权时间</text>
				</view>

				<view class="CardMain">
					<view style="font-weight: 600;">
						<text>{{formData.CreateTime}} 至 {{formData.expireDate}}</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts" name="OnlyIdentity">
	import { reactive, ref } from 'vue';
	import useRequestQueue from '@/uni_modules/mc-ui/libs/http/UniversalRequestQueue';
	const { request, requestByUrl, onAllComplete, onError } = useRequestQueue();
	import { useMainStore, useInventoryStore, useUserStore } from '@/store';
	const inventoryStore = useInventoryStore();
	const mainStore = useMainStore();
	const userStore = useUserStore();

	let formData = reactive({
		OnlyIdentity: '', // 机器码
		IsTrue: false, // 是否授权
		CreateTime: null, // 授权时间
		expireDate: null, // 授权期限
		version: null, // 版本号
	})

	console.log(mainStore.systemConfig.sysDeviceInfo);
	const { appVersion, deviceId } = mainStore.systemConfig.sysDeviceInfo;

	formData.version = appVersion;
	if (deviceId) {
		formData.OnlyIdentity = deviceId;
	};

	// 赋值授权信息
	if (!uni.$utils.isEmpty(mainStore.appConfigInfo.Authorization_Info)) {
		let data = mainStore.appConfigInfo.Authorization_Info;
		formData.IsTrue = data.Authorization;
		formData.CreateTime = data.CreateTime.split('T')[0];
		formData.expireDate = data.EffectiveDate.split('T')[0];
	};

	function ToggleBtn() {
		uni.$utils.showLoading();

		setTimeout(() => {
			uni.$utils.hideLoading();
			if (!formData.IsTrue) {
				apply()
			}
		}, 1000);
	}

	function apply() {
		let param = {
			// cmdname: 'applylicense',
			machineId: formData.OnlyIdentity,
			machineType: 2
		}

		request({
			config: {
			},
			url: 'applylicense',
			method: 'POST',
			data: param,
			success(ret : any) {
				console.log(ret);
				if (ret.status == 200) {
					if (ret.data.ds[0]['status'] == 200) {
						formData.IsTrue = true;
						// 查看本机是否授权
						searchApply();

						uni.showToast({
							title: '授权成功',
						})
						setTimeout(() => {
							uni.hideToast();
							// 重启设备
							// #ifdef APP-PLUS
							plus.runtime.restart();
							// #endif
							// #ifdef APP-HARMONY
							uni.reLaunch({
								url: "/pages/login/login"
							})
							// #endif


						}, 800);
					}
				}
			}
		});
	}

	// 查看本机是否授权
	function searchApply() {
		let param = {
			machineId: formData.OnlyIdentity,
		}

		request({
			config: {
			},
			url: 'searchapply',
			method: 'POST',
			data: param,
			success(ret : any) {
				console.log(ret);
				if (ret.status == 200) {
					if (ret.data.ds.length) {
						// 已授权
						mainStore.appConfigInfo.Authorization_Info.Authorization = true;
						uni.redirectTo({
							url: "/pages/login/login"
						})

					} else { }
				}
			}
		});
	}


	// 退出页面
	function ClosePage() {
		let routes = getCurrentPages(); //当前页面栈
		console.log(routes.length);
		if (routes.length == 1) { // 未授权状态
			uni.setStorageSync('Automatic_Login', false);
			uni.redirectTo({
				url: "/pages/login/login"
			});
		} else {
			uni.navigateBack();
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		// background-color: #f8f8f8;
		background-color: #f5f6fa;
		height: 100vh;
		display: flex;
		flex-direction: column;

		.MainBox {
			flex: 1;
			padding: 0px 15px;

			.CardBox {
				position: relative;
				padding: 15px 10px;
				box-shadow: 1px 1px 13px -10px #999;
				min-height: 60px;
				background-color: #f8f8f8;
				border-radius: 12px;
				margin-top: 10px;

				// 标题样式
				.TitleText {
					display: block;
					font-size: 16px;
					color: #333;
					font-weight: 600;
				}

				// 右上角申请按钮
				.EmpowerBox {
					position: absolute;
					right: 10rpx;
					top: 10rpx;

					.TextBtn:first-child {
						width: fit-content;
						border-radius: 99999px;
						padding: 6px 15px;
						background-color: #51d0d0;
						color: #fff;
					}

					.TextBtn:last-child {
						width: fit-content;
						border-radius: 99999px;
						padding: 6px 15px;
						background-color: #fff;
						color: #51d0d0;
						border: 1px solid #51d0d0;
					}
				}

				.CardMain {
					margin-top: 15px;
					font-size: 14px;

					// 是否授权 标题文字
					.Top_isTrue_Text {
						display: flex;
						align-items: center;

						.isTrue_Text {
							display: block;
							font-size: 14px;
							color: #999;

							.Text {
								display: block;
								font-size: 18px;
								color: #333;
								font-weight: 600;
							}
						}
					}

					// 版本
					.Top_Version_Box {
						margin-top: 12px;

						.Text {
							display: block;
							font-size: 14px;
							color: #333;
							margin-top: 8px;
						}
					}

					// 授权内容状态
					.Top_Status_Box {
						margin-top: 12px;

						.Status_Box {
							display: flex;
							align-items: center;
							margin-top: 8px;
							font-size: 14px;
						}
					}

					// 机器码
					.Top_MachineCode_Box {
						margin-top: 12px;

						.MachineCode_Box {
							display: flex;
							align-items: center;
							margin-top: 8px;
							color: #0647c1;
							font-size: 14px;
						}
					}

				}
			}


		}
	}
</style>